package fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.administrator.car.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import adapter.YouHuiAdapter;
import bean.CarYouHui;
import utils.HttpUtil;

/**
 * Created by Administrator on 2015/4/21.
 */
public class YouHuiFragment extends Fragment{
    private String path="http://mrobot.pcauto.com.cn/v3/price/promotionList?areaId=1&pageNo=1&orderBy=discountRange&pageSize=20";
    private ListView listView;
    private List<CarYouHui> list;
    private YouHuiAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.youhui_fragment,null,false);
        listView = (ListView) v.findViewById(R.id.listView_youhui);
        HttpUtil.getJson(path,new HttpUtil.CallBack() {
            @Override
            public void getResult(String jsonString) {
                Log.i("123--------->",jsonString);
                list = new ArrayList<CarYouHui>();
                CarYouHui car=null;
                try {
                    JSONObject obj=new JSONObject(jsonString);
                    JSONArray array=obj.getJSONArray("data");
                    for (int i = 0; i <array.length() ; i++) {
                        JSONObject obj2=array.getJSONObject(i);
                        car=new CarYouHui();
                        car.setCompName(obj2.getString("dealerName"));
                        car.setCarName(obj2.getString("modelName"));
                        car.setDealerNewsId(obj2.getString("dealerNewsId"));
                        car.setModelId(obj2.getString("modelId"));
                        car.setPrice(obj2.getDouble("modelPrice"));
                        car.setDownPrice(obj2.getDouble("decline"));
                        car.setImg(obj2.getString("image"));
                        car.setArea(obj2.getString("area"));
                        list.add(car);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        listView.setAdapter(new YouHuiAdapter(list,getActivity()));

        return v;
    }
}
